package com.wowangz.cms.systemmanage.sms.query.api;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.utils.DataFormat;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.sms.query.entity.SmsQuery;
import com.wowangz.cms.systemmanage.sms.query.service.SmsQueryService;
@Controller
@Scope("prototype")
public class SmsQueryController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(SmsQueryController.class);
	
	@RequestMapping("/systemmanage/sms/smsQuery/smsQueryView.web")
	public String smsQueryView(HttpServletRequest request,Model model){
		// 获得短信信息的ID
		String id = request.getParameter("id");
		try {
			Map<String, Object> paramerMap = new HashMap<String, Object>();
			paramerMap.put("id", id);
			paramerMap.put("statusid", SysConstant.RecordStatus.VALID);
			SmsQueryService service = (SmsQueryService) IssTransactionContainer.getInstance(SmsQueryService.class, false);
			SmsQuery sms = service.querySmsById(paramerMap);
			model.addAttribute("sms", sms);
		} catch (ServiceException e) {
			logger.error(e.getMessage());
		}
		return "/WEB-INF/jsp/systemmanage/sms/smsQuery/smsQueryView";
	}
	
	
	@RequestMapping("/systemmanage/sms/smsQuery/smsQueryList.web")
	public String smsQueryList(Model model) {
		String starttime = DataFormat.formatDate(DataFormat.getPreQueryDatebox(new Date()), DataFormat.DT_YYYY_MM_DD);
		String endtime = DataFormat.formatDate(new Date(), DataFormat.DT_YYYY_MM_DD);
		model.addAttribute("starttime", starttime);
		model.addAttribute("endtime", endtime);
		return "/WEB-INF/jsp/systemmanage/sms/smsQuery/smsQueryList";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/systemmanage/sms/smsQuery/smsQueryList.json", method = RequestMethod.POST)
	public void smsQueryList(HttpServletRequest request, HttpServletResponse response) {
		SmsQueryService service = (SmsQueryService) IssTransactionContainer.getInstance(SmsQueryService.class, false);
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = new DataGrid();
		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			Map<String, Object> map = (Map<String, Object>) ServletUtils.getParametersToHashMap(request);
			dataGrid.setPangingParam(pangingParam);
			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			map.put("createtimeto", DataFormat.getEndtimetString(request.getParameter("createtimeto")));
			long resultCount = service.querySmsCount(map);
			List<SmsQuery> resultList = service.querySmsPage(map);
			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);
			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("sendstatus", DataTypeConstant.LONG);
			dataStyle.addDepict("smscontent", DataTypeConstant.STRING);
			dataStyle.addDepict("telephone", DataTypeConstant.STRING);
			dataStyle.addDepict("acceptusername", DataTypeConstant.STRING);
			dataStyle.addDepict("inputtime", DataTypeConstant.DATETIME);
			dataStyle.addDepictByFunction("sendstatusname", SysConstant.Sendstatus.class, "getName", new Class[] { long.class });
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
